<?php
include_once 'TableRow.php';

class Table {

    private $name;
    
    private $cols = array();
    
    /**
     *
     * @var TableRow
     */
    private $rows = array();
    
    //formato
    private $width = '100%';
    
    private $style;
    

    public function __construct($name) {
        $this->name = $name;
    }
    
    public function __toString() {
        return (String) $this->_generateHtml();
    }

    private function _generateHtml() {

        //recupero datos
        
        $cols = $this->cols;
        
        $width = $this->width;

        $html = array();
        
        //genero el html de la tabla
        $html[] = "<table class='".$this->style."' name='" . $this->name . "' id='" . $this->name . "' width='$width'>";

        //imprimo columnas
        if ($cols) {
            $html[] = '<thead style="display:table-header-group"><tr>';
            foreach ($cols as $col) {
                $html[] = '<th width="' . $col[1] . '" colspan="' . $col[2] .  '">' . $col[0] . '</th>';
            }
            $html[] = '</tr></thead><tbody style="height:100px;overflow:auto">';
        }

        $rows = $this->rows;
        foreach ($rows as $row) {
            
            $html[] = $row->generate();
        }

        $html[] = '</tbody></table>';
        
        $output = '';
        foreach ($html as $code) {
            $output = $output . $code;
        }
        return (String) $output;
    }

    public function generate() {
        return$this->_generateHtml();
    }

    public function addCol($label, $width = NULL, $colspan = NULL) {
        $this->cols[] = array($label, $width, $colspan);
    }
    
    public function addRow(TableRow $row) {
        $this->rows[] = $row;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function getWidth() {
        return $this->width;
    }

    public function setWidth($width) {
        $this->width = $width;
    }
    
    public function setStyle($style) {
        $this->style = $style;
    }




}

?>
